#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct ipAddr {
    int addr[4];
} IP;

int indexOf(IP* proxy, IP n, int length) {
    for (int i = 0; i < length; i++) {
        if (proxy[i].addr[0] == n.addr[0] && proxy[i].addr[1] == n.addr[1] &&
            proxy[i].addr[2] == n.addr[2] && proxy[i].addr[3] == n.addr[3])
            return i;
    }
    return -1;
}

void helper(IP* proxy, IP* server, int length1, int length2) {
    //代理地址的数组 & 服务请求地址数组
    int* occu = (int*)malloc(length1 * sizeof(int));
    memset(occu, 0, length1 * sizeof(int));
    //服务器地址的长度
    int count = 1, ret = 0, p = 1;  // count-计数 ret-返回的值 p-指针
    int indexInit = indexOf(proxy, server[0], length1);
    occu[indexInit] = 1;
    while (p < length2) {
        int index = indexOf(proxy, server[p], length1);
        if (index > -1 && occu[index] != 1) {
            if (count == length1 - 1) {
                ret++;
                memset(occu, 0, length1 * sizeof(int));
                count = 1;
            } else {
                count++;
            }
            occu[index] = 1;
        }
        p++;
    }
    printf("%d\n", ret == 0 ? -1 : ret);
}

int main() {
    int n, m;
    FILE* fp = fopen("./data.in", "r");
    fscanf(fp, "%d\n", &n);
    IP* proxy = (IP*)malloc(sizeof(IP) * n);
    for (int i = 0; i < n; i++) {
        fscanf(fp, "%d.%d.%d.%d\n", &proxy[i].addr[0], &proxy[i].addr[1],
               &proxy[i].addr[2], &proxy[i].addr[3]);
    }
    fscanf(fp, "%d\n", &m);
    IP* server = (IP*)malloc(sizeof(IP) * m);
    for (int i = 0; i < m; i++) {
        fscanf(fp, "%d.%d.%d.%d\n", &server[i].addr[0], &server[i].addr[1],
               &server[i].addr[2], &server[i].addr[3]);
    }
    helper(proxy, server, n, m);
}